package app.im;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import model.EmployeeItemDBModel;

/**
 * Created by ThinkPad on 2017/5/15.
 */

public class IMAddChatModel {

    public IMAddChatModel(){

    }

    public String deptID;
    public String title;

    private String nowDeptID = null;
    private Stack<String> stackDeptID = new Stack<String>();

    /**
     * 保存此次获取list的id记录
     * @param deptID
     */
    public void setDeptID(String deptID){
        if(!TextUtils.isEmpty(nowDeptID)){
            stackDeptID.push(nowDeptID);
        }
        nowDeptID = deptID;
    }

    /**
     * 获取上次的id记录 没有则返回null
     * @return
     */
    public String backDeptID(){
        if(stackDeptID.empty()){
            nowDeptID = null;
        }else{
            nowDeptID = stackDeptID.pop();
        }
        return nowDeptID;
    }

    @NonNull
    public List<EmployeeItemDBModel> getEmployeeItemDBModels(JSONObject jsonResult) throws JSONException {
        List<EmployeeItemDBModel> listData = new ArrayList<EmployeeItemDBModel>();
        String CurrentDeptName = jsonResult.getString("CurrentDeptName");
        JSONArray Depts = jsonResult.getJSONArray("Depts");
        JSONArray Empls = jsonResult.getJSONArray("Empls");

        if (Depts.length() > 0) {
            listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, "部门"));
        }
        for (int i = 0; i < Depts.length(); i++) {
            JSONObject deptObj = Depts.getJSONObject(i);
            String ID = deptObj.getString("ID");
            String Name = deptObj.getString("Name");

            EmployeeItemDBModel item = new EmployeeItemDBModel();
            item.DeptID = ID;
            item.EmplID = null;
            item.Name = Name;
            item.type = EmployeeItemDBModel.ITEM;
            listData.add(item);
        }
        if (Empls.length() > 0) {
            listData.add(new EmployeeItemDBModel(EmployeeItemDBModel.SECTION, "人员"));
        }
        for (int i = 0; i < Empls.length(); i++) {
            JSONObject emplObj = Empls.getJSONObject(i);
            String ID = emplObj.getString("ID");
            String Name = emplObj.getString("Name");
            String DeptID = emplObj.getString("DeptID");
            String Sex = emplObj.getString("Sex");

            EmployeeItemDBModel item = new EmployeeItemDBModel();
            item.EmplID = ID;
            item.DeptID = null;
            item.Name = Name;
            item.type = EmployeeItemDBModel.ITEM;
            listData.add(item);
        }
        return listData;
    }
}
